package cn.itcust.datasource.druid;

import cn.itcust.datasource.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * 使用新的工具类
 */
public class DruidDemo2 {
    public static void main(String[] args) {
        // 完成添加操作，给stu表添加一条数据
        Connection conn = null;
        PreparedStatement pstmt = null;
        try {
            // 1. 获取连接
            conn = JDBCUtils.getConnection();
            // 2. 定义sql
            String sql = "insert into stu values(null, ?, ?, ?, ?, null, null)";
            // 3. 获取pstmt对象
            pstmt = conn.prepareStatement(sql);
            // 4. 给？赋值
            pstmt.setString(1, "腾翔");
            pstmt.setInt(2, 20);
            pstmt.setDouble(3, 84);
            pstmt.setDate(4, Date.valueOf("2019-09-29"));
            // 5. 执行sql
            int count = pstmt.executeUpdate();
            System.out.println(count);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 6. 释放资源
            JDBCUtils.close(pstmt, conn);
        }
    }
}
